@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS
SET /A CYLINDERS=0
SET /A HEADS=255
SET /A SECTORS=63
SET /A STARTsector=0
SET /A ENDsector=0

ECHO.
ECHO dd2vmdk.cmd: small batch file to create a .vdmk descriptor file
ECHO              for RAW disk images to be used with VMware to 
ECHO              mount the image with the correct geometry
ECHO Author:      Jean-Francois Gingras <jean.francois.gingras@gmail.com>
ECHO.
ECHO Based on the script 'makepln' from jaclaz http://home.graffiti.net/jaclaz:graffiti.net/
ECHO.

SET /A HEADS=255
SET /A SECTORS=63

IF "%~1."=="." GOTO :Interactive
IF NOT EXIST "%~1" ECHO FILE NOT FOUND &GOTO :Interactive
CALL :Setvars %1
IF NOT %2.==. SET HEADS=%2
IF NOT %3.==. SET SECTORS=%3
GOTO :DOIT

:Interactive
ECHO Please input filename of the imagefile:
SET /P imgfile=
ECHO Imgfile is %imgfile%
CALL :Setvars %imgfile%
IF NOT EXIST %imgP%%imgN%%imgE% ECHO File NOT FOUND &GOTO :Interactive
ECHO Please input number of Heads for the imagefile [ENTER] for default 255:
SET /P HEADS=
REM SET /A HEADS=%HEADS%
ECHO Please input number of Sectors for the imagefile [ENTER] for default 63:
SET /P SECTORS=
REM SET /A SECTORS=%SECTORS%

:DOIT
(
ECHO version=1
ECHO encoding="windows-1252"
ECHO CID=fffffffe
ECHO parentCID=ffffffff
ECHO createType="twoGbMaxExtentFlat"
ECHO.
) > "%imgN%.vmdk"
FOR /F "tokens=3,4 delims= " %%A IN ('DIR /-C "%imgP%%imgN%.???" ^|FIND /I "%imgN%."') DO @CALL :Writefile %%A %%B
GOTO :WriteEOF

:Setvars
%~d1
CD %~dp1
SET imgN=%~n1
SET imgE=%~x1
SET imgP=%~dp1
GOTO :EOF

:Writefile
SET /A SIZEsectors=%1/512
SET /A ENDsector=%STARTsector%+%SIZEsectors%
ECHO %2 start: %STARTsector% end: %ENDsector%
CALL :Setvars %2
(
ECHO RDONLY %SIZEsectors% FLAT "%imgN%%imgE%" 0
) >> "%imgN%.vmdk"
SET /A STARTsector=%ENDsector%
SET /A CYLINDERS=%ENDsector%
GOTO :EOF

:WriteEOF
SET /A CYLINDERS=%CYLINDERS%/%SECTORS%/%HEADS%
(
ECHO.
ECHO ddb.geometry.cylinders = %CYLINDERS%
ECHO ddb.geometry.heads = %HEADS%
ECHO ddb.geometry.sectors = %SECTORS%
ECHO ddb.adapterType = "ide"
) >> "%imgN%.vmdk"
GOTO :EOF

:Usage
ECHO Usage:
ECHO makepln.cmd [filename] [heads] [sectors]
ECHO Examples:
ECHO makepln.cmd C:\testimg.img 16 63
ECHO makepln.cmd C:\testimg.img (will default to Nx255x63)
ECHO Long filenames are OK, as long as given inside double quotes
ECHO If no parameters are given, will enter interactive mode
GOTO :EOF